package com.example.admin.testdemo.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.admin.testdemo.R;
import com.example.admin.testdemo.rs232.Command;
import com.example.admin.testdemo.rs232.Serial;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class WeightDeviceActivity extends AppCompatActivity implements View.OnClickListener {

    private static String TAG = "WeightDeviceActivity";
    private Button btn_dzs;
    private Button btn_dzso;
    private boolean dzsFlag = false;
    private boolean btnDzsFlag = true;
    private Serial serial = Serial.getSerialInstance();
    private EditText et_recive_dzs;
    private ScheduledExecutorService scheduledThreadPool;
    private int recieveFlag = 0;
    byte[] dzsData = new byte[]{(byte) 0x3B, (byte) 0xB3, (byte) 0x00, (byte) 0x03, (byte) 0xA5,
            (byte) 0x10, (byte) 0x10, (byte) 0x2E};
    private Serial.DataCallback callback = new Serial.DataCallback() {
        @Override
        public void onDataReceived(byte[] buffer, int size) {
            Log.e(TAG, "接收：" + Command.printByteInfo(buffer, size));
            if (size <= 8 && buffer[13] == (byte) 0x00) {
//                recieveFlag++;
//                setEt(et_recive_dzs, Integer.toString(recieveFlag));
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weight_device);
        initData();
        initView();
        initTimer();

    }

    private void initData() {
        serial.setCallback(callback);
    }

    private void initView() {
        et_recive_dzs = findViewById(R.id.et_recive_dzs);
        btn_dzs = findViewById(R.id.btn_dzs);
        btn_dzs.setOnClickListener(this);
        btn_dzso = findViewById(R.id.btn_dzso);
        btn_dzso.setOnClickListener(this);
    }


    private void initTimer() {
        if (scheduledThreadPool == null) {
            scheduledThreadPool = new ScheduledThreadPoolExecutor(1);
        }
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (dzsFlag) {
                    serial.sendData(dzsData);
                    recieveFlag++;
                    setEt(et_recive_dzs, Integer.toString(recieveFlag));
                }
            }
        }, 0, 3000, TimeUnit.MILLISECONDS);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dzs:
                testDzs();
                break;
            case R.id.btn_dzso:
                dzsFlag=false;
                et_recive_dzs.setText("0");
                serial.sendData(dzsData);
                break;

            default:
        }
    }

    private void testDzs() {
        if (btnDzsFlag) {
            btn_dzs.setText("测试中");
            et_recive_dzs.setText("0");
            dzsFlag = true;
            btnDzsFlag = false;
        } else {
            btn_dzs.setText("测试");
            dzsFlag = false;
            btnDzsFlag = true;
        }
    }

    private void setEt(final EditText et, final String flag) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                et.setText(flag);
            }
        });
    }
}
